package com.colt.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/*
* 此类必须以GatewayFilterFactory结尾
*
*
* 自定义过滤器
* */
@Slf4j
@Component
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {

        return ((exchange, chain) -> {
            log.info("你请求到我了！！{},{}",config.getName(),config.getValue());
            //被修改的 请求
            ServerHttpRequest modifiedRequest = exchange.getRequest()
                    .mutate()
                    .build();
            ServerWebExchange modifiedExchaneg = exchange.mutate()
                    .request(modifiedRequest)
                    .build();
            //将修改好的请求传给下一个过滤器
            return chain.filter(modifiedExchaneg);
        });
    }
}
